package com.geek.novel.fragment

import android.annotation.SuppressLint
import android.os.Bundle
import android.view.View
import android.view.ViewGroup
import android.widget.BaseAdapter
import com.geek.novel.R
import com.geek.novel.activity.book.BookDetailActivity
import com.geek.novel.adapter.RecommendAdapter
import com.geek.novel.adapter.TodayBookAdapter
import com.geek.novel.bo.AppContext
import com.geek.novel.common.Constant
import com.geek.novel.entity.BookRead
import com.geek.novel.network.RslHandler
import com.geek.novel.services.NovelService
import com.geek.novel.view.list.AllListView
import com.geek.novel.vo.ApiErrorResult
import com.geek.novel.vo.ApiResult
import com.geek.novel.vo.BookStoreVo
import kotlinx.android.synthetic.main.fragment_book_shop.*
import kotlinx.android.synthetic.main.layout_book_shop_recommend.*
import kotlinx.android.synthetic.main.layout_book_shop_today.*


/**
 * 小说推荐页面
 */
class FragmentBookShop : BaseFragment(), RslHandler {

	/**推荐书籍*/
	private val recommendBookList = ArrayList<BookRead>()

	/**今日必读*/
	private val todayBookList = ArrayList<BookRead>()

	/**小说服务*/
	private lateinit var novelService: NovelService

	override fun layoutId(): Int = R.layout.fragment_book_shop
	override fun pageName(): String = AppContext.getContext().getString(R.string.app_name)
	override fun fragmentName(): String = FragmentName

	override fun initV() {
		//初始化服务对象
		novelService = NovelService(this)

		//发起请求获取首页推荐小说
		novelService.getRecommendBooks()

		//初始化下拉刷新功能
		initRefreshLayout()

		//绑定顶部的推荐列表数据
		bindRecommendBooksView()
		//绑定今日必读列表数据
		bindTodayBooksView()
	}

	/***
	 * 绑定顶部的推荐列表数据
	 */
	private fun bindRecommendBooksView(){
		//顶部的推荐列表
		wGallery.adapter = RecommendAdapter(recommendBookList)
		wGallery.setOnItemClickListener { _, _, position, _ ->
			//前往书籍详情页面
			gotoBookDetailPage(recommendBookList[position])
		}
	}

	/***
	 * 绑定今日必读列表数据
	 */
	private fun bindTodayBooksView(){
		//今日必读列表
		lv_all_book_today.setAdapter(TodayBookAdapter(todayBookList))
		lv_all_book_today.setOnItemClickListener(object: AllListView.OnItemClickListener{
			override fun onItemClick(parent: ViewGroup?, view: View?, position: Int, id: Long) {
				//前往书籍详情页面
				gotoBookDetailPage(todayBookList[position])
			}
		})
	}

	/**
	 * 前往书籍详情页面
	 */
	private fun gotoBookDetailPage(book: BookRead){
		val params = Bundle()
		params.putString("bookId", book.bookId)
		startActivityByAnim(requireContext(), BookDetailActivity::class.java, params)
	}

	/**
	 * 初始化下拉刷新功能
	 */
	private fun initRefreshLayout(){
		refreshLayout.setRefreshHeader(classicsHeader)
		refreshLayout.setEnableLoadMore(false)//不开启滚动加载
		refreshLayout.setOnRefreshListener { _ ->
			//发起请求获取首页推荐小说
			novelService.getRecommendBooks()
		}
	}


	override fun myClick(v: View) {
	}

	override fun onError(code: Int, err: ApiErrorResult) {
		when(code){
			//获取推荐书籍结果
			Constant.HTTP_KEY_APP_GET_RECOMMEND_BOOK_LIST->{
				//更新滚动加载的状态
				refreshLayout.finishRefresh(false)
			}
		}
	}
	override fun onSuccess(result: ApiResult, code: Int) {
		when(code){
			//获取推荐书籍结果
			Constant.HTTP_KEY_APP_GET_RECOMMEND_BOOK_LIST->{
				//更新滚动加载的状态
				refreshLayout.finishRefresh(true)

				val bookStoreVo = result.getData() as BookStoreVo

				//推荐列表
				val recommendList = bookStoreVo.recommendList
				recommendBookList.clear()
				if(recommendList != null){
					recommendBookList.addAll(recommendList)
				}
				val galleryAdapter = wGallery.adapter as BaseAdapter
				galleryAdapter.notifyDataSetChanged()

				//今日必读
				val todayList = bookStoreVo.todayList
				todayBookList.clear()
				if(todayList != null){
					todayBookList.addAll(todayList)
				}
				val adapter = lv_all_book_today.getAdapter()
				adapter!!.notifyDataSetChanged()
			}
		}
	}

	companion object {
		const val FragmentName = "FragmentBookShop"
		/**单例instance*/
		@SuppressLint("StaticFieldLeak")
		private var instance: FragmentBookShop? = null
		fun newInstance(): FragmentBookShop{
			if(instance == null){
				instance = FragmentBookShop()
			}
			return instance!!
		}
	}

}
